<template>
  <a-modal
    title="同步文件"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label="actionType === 3?'图片分组':'音视分组'" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            @search="getDirOptions"
            @change='getSyncNum'
            :options='dirList'
            :filter-option="false"
            showSearch
            v-decorator="['dir', { initialValue: '' ,rules: [{required: true}]}]">
            <template v-if="fetching" #notFoundContent>
              <a-spin size="small" />
            </template>
          </a-select>
        </a-form-item>
        <ul >
          <li v-for='item in infoList' class='text-flex'>
            <b>{{item.label}}:</b> <span>{{item.value}}</span>
          </li>
        </ul>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import * as Api from '@/api/book/page'
export default {
  components: {
  },
  data () {
    return {
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      dirList:[],
      bookId:'',
      targetId:'',
      fetching:false,
      actionType:-1,
      infoList:[]
    }
  },
  methods: {

    /**
     * 显示对话框
     */
    open (bookId,type) {
      // 显示窗口
      this.visible = true
      this.bookId = bookId
      this.actionType = type
      this.targetId = ''
      this.infoList=[]
      this.dirList=[]
      this.getDirOptions()
    },
    getSyncNum(targetId){
      this.confirmLoading=true
      this.targetId = targetId
      Api.getSyncNum({bookId:this.bookId,targetId:this.targetId,type:this.actionType}).then(res=>{
        this.infoList = res.data
      }).finally(()=>{
        this.confirmLoading = false
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.confirmLoading=true
          Api.syncFile({bookId:this.bookId,targetId:this.targetId,type:this.actionType}).then(res=>{
            // 显示成功
            this.$message.success(res.message, 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit')
          }).finally(()=>{
            this.confirmLoading = false
          })
        }
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
    },
    getDirOptions(name)
    {
      this.fetching=true
      Api.getDirOptions({name:name,type:this.actionType ===3?1:0}).then(res=>{
        this.dirList=res.data
      }).finally(()=>{
        this.fetching=false
      })
    }
  }
}
</script>
<style scoped>
.sync-info{
  font-size: 18px;
  font-weight: bold;
  text-align: center;
}

.text-flex {
  margin: auto auto 3px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 250px;
  font-size: 18px;
  font-weight: bold;
}

.text-flex > b{
  font-weight: normal;
  width: 100%;
  word-break: break-all;
}
.text-flex > span {
  text-align: right;
  width: 100%;
}
</style>
